package com.are.odisea;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Insert
 */
@WebServlet("/Insert")
public class Insert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Insert() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void ProcesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	
    	PrintWriter out = response.getWriter();	

    	String usuario=(String)request.getParameter("usuario");
    	String clave=(String)request.getParameter("clave");
    	String perfil=(String)request.getParameter("perfil");
    	String nombre=(String)request.getParameter("nombre");
    	String estado=(String)request.getParameter("estado");
    	
    	db conexion = null;
    	try {
			conexion = new db();
			String sql = "insert into prueba (usuario,clave,perfil,nombre,estado) values (?,?,?,?,?)";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, usuario);
			pst.setString(2, clave);
			pst.setString(3, perfil);
			pst.setString(4, nombre);
			pst.setString(5, estado);
			
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				out.print("OK");
			}else{
				out.print("Se produjo un error al insertar");	
				}
					
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (conexion != null){
				try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					//out.print("NO se conecto a la base de datps");
					e.printStackTrace();
				}
			}
		}
    	
    	
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 ProcesarPeticion(request, response); 
	}

}
